package com.example.service.impl;

import com.alibaba.fastjson.annotation.JSONField;
import com.example.entity.SysUser;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author: 21050134
 * @date: 2023/11/8 16:25
 * @description:
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class LoginUser implements UserDetails , Serializable {

	private SysUser user;

	//存放当前登录用户的权限信息，一个用户可以有多个权限
	private List<String> permissions;

	public LoginUser(SysUser user, List<String> permissions) {
		this.user = user;
		this.permissions = permissions;
	}
	//权限集合
	@JSONField(serialize = false)
	private  List<SimpleGrantedAuthority>  authorities;

	//获取权限信息
	@Override
	public Collection<? extends GrantedAuthority> getAuthorities() {
		if (authorities!=null){
			return authorities;
		}
		//把permissions中String类型的权限信息封装成SimpleGrantedAuthority
		//第一种方式
//         List<GrantedAuthority> newList = new ArrayList<>();
//        for (String permission : permissions) {
//            SimpleGrantedAuthority authority = new SimpleGrantedAuthority(permission);
//            newList.add(authority);
//        }

		//方式二
		authorities = permissions.stream().
				map(SimpleGrantedAuthority::new).
				collect(Collectors.toList());

		return authorities;

	}

	@Override
	public String getPassword() {
		return user.getPassword();
	}

	@Override
	public String getUsername() {
		return user.getUserName();
	}

	@Override
	public boolean isAccountNonExpired() {
		return true;
	}

	@Override
	public boolean isAccountNonLocked() {
		return true;
	}

	@Override
	public boolean isCredentialsNonExpired() {
		return true;
	}

	@Override
	public boolean isEnabled() {
		return true;
	}
}
